# import requests
# from lxml import etree
import os
import shutil
if os.path.exists('static/千叶88'):
    shutil.rmtree('static/千叶88')
os.mkdir('static/千叶88')

#
# url = 'http://qianye88.com/cate8/1.html'
#
#
#
# res = requests.get(url,)
#
# text = res.text
# html_info = etree.HTML(text)
# data_info = html_info.xpath('//div[@class="item"]/a[@class="title"]')
# for data in data_info:
#     title = data.xpath('.//text()')[0]
#     link = data.xpath('.//@href')[0]
#     total_link = 'http://qianye88.com' + link
#     img_res = requests.get(total_link)
#     img_text = img_res.text
#     html_info = etree.HTML(img_text)
#     img_url = html_info.xpath('//img[@class="content-img startlogin"]/@src')
#     datail_img = requests.get(img_url[0])
#     with open(f'static/千叶88/{title}.jpg', 'wb') as f:
#         f.write(datail_img.content)


import asyncio
import aiohttp
from lxml import etree

async def fetch(session, url):
    async with session.get(url) as response:
        text = await response.text()
        return text

async def main():
    async with aiohttp.ClientSession() as session:
        text = await fetch(session, 'http://qianye88.com/cate8/1.html')
        html_info = etree.HTML(text)
        data_info = html_info.xpath('//div[@class="item"]/a[@class="title"]')
        for data in data_info:
            title = data.xpath('.//text()')[0]
            link = data.xpath('.//@href')[0]
            total_link = 'http://qianye88.com' + link
            img_text = await fetch(session, total_link)
            html_info = etree.HTML(img_text)
            img_url = html_info.xpath('//img[@class="content-img startlogin"]/@src')
            async with session.get(img_url[0]) as datail_img :
                content = await datail_img.content.read()
            with open(f'static/千叶88/{title}.jpg', 'wb') as f:
                f.write(content)


asyncio.run(main())

    
